package com.wanmait.demo.webcontroller.usercontroller;


import com.wanmait.demo.pojo.Userinfo;
import com.wanmait.demo.service.UserService;
import com.wanmait.demo.util.CookieUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//尹山坤
@Controller
@RequestMapping("/user")
public class Usercontroller {
    //尹山坤
    @Resource
    private UserService userService;
    @RequestMapping("/ceshi")
    public String ceshi(){
        return "user/ysk/ceshi";
    }


    @RequestMapping("/changepasswordpager")  //修改密码界面尹山坤
    public String changepasswordpager(){
        return "user/ysk/changepassword";
    }




    //尹山坤  修改密码
    @RequestMapping("/changeuserpass")
    public String  ChangeUserpass(HttpSession session, String userPass, HttpServletResponse response, String phonenum, String phonecode) {
        //获取用户



           String  telnum = new Md5Hash(phonenum, "", 5).toString();

        Userinfo userinfo = (Userinfo) session.getAttribute("loginUser");
        System.out.println(userinfo.getPhoneNumber());
        if (userinfo.getPhoneNumber().equals(telnum)) { //输入手机号和用户手机号需要一致

            String code = (String) session.getAttribute("phonennn");
            if (code.equals(phonecode)) { //if验证码正确

                Md5Hash md5Hash = new Md5Hash(userPass, "", 5);//userPass  新密码 jsp
                String newpass = md5Hash.toString();//新密码加密后toString
                System.out.println(newpass);
                //获取新密码

                userService.updatauserpass(userinfo.getId(), newpass);
                System.out.println("密码修改成功");
                //调用根据用户id修改密码的方法

                CookieUtils.write("username", "", "/", 0, response);
                CookieUtils.write("password", "", "/", 0, response);
                return "user/ysk/login";
            } else {
                return "redirect:/user/changepasswordpager?errMes=2";  //验证码不正确返回修改密码页面  加点提示
            }
        } else {

            return "redirect:/user/changepasswordpager?errMes=1";//提示手机号错误
        }


//
  }

@RequestMapping("/underway")
    public String underway(){
        return "user/ysk/underway";
}

}
